package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录方法")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder().id(employee.getId()).userName(employee.getUsername()).name(employee.getName()).token(token).build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工登出方法")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * @param employeeDTO :前端传递DTO对象
     */
    @PostMapping
    @ApiOperation(value = "新增员工方法")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        // 调用Service层方法 新增员工
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /*员工分页查询*/
    @ApiOperation(value = "员工分页查询方法")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        // 调用Service层分页查询方法
        log.info("员工分页查询，参数{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /*启用 / 禁用 员工账号*/
    @PostMapping("status/{status}")
    @ApiOperation(value = "员工状态修改方法")
    public Result enableOrBan(@PathVariable(value = "status") Integer status, Long id) {
        log.info("启用 / 禁用员工账号,修改状态为:{},修改员工id为{}", status, id);
        /*调用service层启动禁用方法*/
        employeeService.enableOrBan(status, id);
        return Result.success("修改成功");
    }

    /*根据id查询员工*/
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据员工id查询员工信息")
    public Result<Employee> getEmployeeById(@PathVariable Long id) {
        // 调用Service层查询方法
        log.info("根据员工id{}查询员工信息", id);
        Employee employee = employeeService.getEmployeeById(id);
        return Result.success(employee);
    }

    /*修改员工信息*/
    @PutMapping(value = "")
    @ApiOperation(value = "修改员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        //调用Service层修改员工方法
        log.info("修改员工信息,参数为{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
